package com.whzykj.resource;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.whzykj.common.DictCache;
import com.whzykj.common.JsonUtil;
import com.whzykj.common.ReturnEnum;
import com.whzykj.model.Account;

import net.sf.json.JSONObject;

public class AcctResource {
	/****
	 * 余额接口
	 * @param messageObj
	 * @param result
	 * http://localhost:8080/bcy/mobile/Interface?content={namespace:'acct',type:'balance',cust_id:'52'}
	 */
	public void balance(JSONObject messageObj, JSONObject result) {
		String cust_id=messageObj.getString("cust_id");//手机号
		String acctsql=" SELECT * FROM account WHERE cust_id=? ";
		Record rd = Db.findFirst(acctsql,cust_id);
		//判断账户是否存在
		if(rd == null)
		{
			JsonUtil.put(result, ReturnEnum.NO_ACCT);
			return;
		} else {
			String state = rd.getStr("state");
			if ("01".equals(state))// 冻结
			{
				JsonUtil.put(result, ReturnEnum.NO_ACCT_FREEZE);
				return;
			} else if ("02".equals(state)) {// 注销
				JsonUtil.put(result, ReturnEnum.NO_ACCT_CLOSE);
				return;
			} else {
				
				result.putAll(JsonUtil.getMapByJfinalRecord(rd));
				JsonUtil.put(result, ReturnEnum.BALANCE_QUERY_SUCCESS);
			}
		}
	
	}
	
	/****
	 * 账单列表接口
	 * @param messageObj
	 * @param result
	 * http://localhost:8080/bcy/mobile/Interface?content={namespace:'acct',type:'billdetail',cust_id:'52',page_current:'1',page_size:'5'}
	 */
	public void billdetail(JSONObject messageObj, JSONObject result) {
		
		String pageCurrent = messageObj.getString("page_current");
        if(pageCurrent == null || "".equals(pageCurrent))
        {
        	pageCurrent="1";
        }
        String page_size = messageObj.getString("page_size");
        if(page_size == null || "".equals(page_size))
        {
        	page_size="50";
        }
        
		
		String cust_id=messageObj.getString("cust_id");//手机号
		String acctsql=" SELECT * FROM account WHERE cust_id=? ";
		Record rd = Db.findFirst(acctsql,cust_id);
		//判断账户是否存在
		if(rd == null)
		{
			JsonUtil.put(result, ReturnEnum.NO_ACCT);
			return;
		} else {
			String state = rd.getStr("state");
			if ("01".equals(state))// 冻结
			{
				JsonUtil.put(result, ReturnEnum.NO_ACCT_FREEZE);
				return;
			} else if ("02".equals(state)) {// 注销
				JsonUtil.put(result, ReturnEnum.NO_ACCT_CLOSE);
				return;
			} else {
				
				int acct_id = rd.getInt("acct_id");//账户ID
				
				String sqlBefore = " select *  ";
				StringBuffer sqlAfter = new StringBuffer(" from Account_Seq where acct_id ="+acct_id+" order by create_time desc,change_type DESC ");
				
				Page<Record> paginate = Db.paginate(Integer.parseInt(pageCurrent), Integer.parseInt(page_size), sqlBefore,sqlAfter.toString());
				List<Record> rdList =paginate.getList();
				if (rdList == null || rdList.size() == 0) {
					JsonUtil.put(result, ReturnEnum.RETURN_SUCCESS_NODATA);
				} else {
					JsonUtil.put(result, ReturnEnum.RETURN_SUCCESS);
					
					for (Record acctseq : rdList) {

						BigDecimal pre_amount = acctseq
								.getBigDecimal("pre_amount");// 变动前总金额
						BigDecimal amount = acctseq.getBigDecimal("amount");// 变动后总金额

						String seq_flag = acctseq.getStr("seq_flag");
						if ("00".equals(seq_flag)) {
							BigDecimal fix_amount = amount.subtract(pre_amount);
							acctseq.set("fix_amount", "+" + fix_amount);
						} else if ("01".equals(seq_flag)) {
							BigDecimal fix_amount = pre_amount.subtract(amount);
							acctseq.set("fix_amount", "-" + fix_amount);
						}

						String change_type = acctseq.getStr("change_type");
						String change_typename = DictCache.cache
								.getDicByText(change_type);
						acctseq.set("change_typename", change_typename);
					}
					result.put("data", JsonUtil.getJsonObjByjfinalList(rdList));

				}
			}
		}
	
	}
}
